package com.he.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.he.pojo.PermissionDetail;
import com.he.pojo.PermissionDetailExample;
import com.he.pojo.User;
import com.he.pojo.UserLog;
import com.he.service.department.DepartmentServiceI;
import com.he.service.permissionDetail.PermissionDetailServiceI;
import com.he.service.role.RoleServiceI;
import com.he.service.user.UserServiceI;
import com.he.service.userLog.UserLogServiceI;
import com.he.util.ImgUtil;
/**
 * 
 * @类名：LoginController
 * @描述：有权限登录
 * @作者：张赫赫
 * @日期：2018年8月27日 下午3:06:35
 */
@Controller
public class LoginController {
	@Autowired
	UserServiceI userService;
	@Autowired
	DepartmentServiceI departService;
	@Autowired
	PermissionDetailServiceI permissDetailService;
	@Autowired
	private UserLogServiceI ulsi;
	@Autowired
	RoleServiceI roleService;
	@RequestMapping("login")
	@ResponseBody
	public String login(User user,String code,HttpServletRequest request) {
		User user2 = userService.login(user);
		String rcode=(String) request.getSession().getAttribute("code");
		rcode=rcode.toLowerCase();
		code=code.toLowerCase();
		if(user2!=null&&rcode.equals(code)) {
			HttpSession session=request.getSession();
			session.setAttribute("user", user2);
			user2.setDepart(departService.getDepartmentById(user2.getDepartmentId()).getDeptName());
			user2.setRole(roleService.getRoleById(user2.getRoleId()).getRoleName());
			return "1";
		}else if (user2==null) {
			return "*用户名或密码错误";
		}else {
			return "*验证码错误";
		}
	}
	@RequestMapping("code")
	public void code (HttpServletRequest request ,HttpServletResponse response) throws IOException {
		char[] array=ImgUtil.getStr();
		//产生验证码后将验证码存入Session方便后面验证
		request.getSession().setAttribute("code", String.valueOf(array));
		BufferedImage image =ImgUtil.getImage(array);
		ImageIO.write(image, "JPG", response.getOutputStream());
	}
	@RequestMapping("index")
	public String index(HttpServletRequest request,ModelMap map) {
		HttpSession session=request.getSession();
		if("out".equals(request.getParameter("flag"))) {
			System.out.println("退出登录");
			session.invalidate();
			return "../../login";
		}
		User user=(User) session.getAttribute("user");
		if(user!=null) {
			PermissionDetail permissionDetail=new PermissionDetail();
			permissionDetail.setWithPage(0);
			PermissionDetailExample example=new PermissionDetailExample();
			example.createCriteria().andRoleIdEqualTo(user.getRoleId());
			List<PermissionDetail> details = permissDetailService.getPermissionDetails(example, permissionDetail);
			map.addAttribute("details", details);
			return "index";
		}else {
			return "../../login";
		}
	}
}
